<?php

class Sms {

    //public $gatewayUrl = "http://sms.1xinxi.cn/asmx/smsservice.aspx?";
    public $gatewayUrl = "http://sdk.univetro.com.cn:6200/sdk/SDKService";
    public $username;
    public $userpwd;
    public $sign;

    public function __construct($username = '13777867486', $userpwd = '3F202ADF6B3414648990722D035D', $sign = '杭州宝泽') {
        $this->username = $username;
        $this->userpwd = $userpwd;
        $this->sign = $sign;
        define('SCRIPT_ROOT', dirname(__FILE__) . '/');
        require_once SCRIPT_ROOT . 'include/Client.php';
        //连接超时时间，单位为秒
        $connectTimeOut = 2;
        //远程信息读取超时时间，单位为秒
        $readTimeOut = 10;
        $proxyhost = false;
        $proxyport = false;
        $proxyusername = false;
        $proxypassword = false;
        $this->_client = new SmsClient($this->gatewayUrl, '7SDK-LHW-0588-QFWUM', '732538', 'ningboyihao2015', $proxyhost, $proxyport, $proxyusername, $proxypassword, $connectTimeOut, $readTimeOut);
        $this->_client->setOutgoingEncoding("UTF-8");
    }

    public function sendsms($mobile, $content)
    {
        if(is_numeric($mobile)) $mobile = array($mobile);
        $k_arr = array(
            '贷款' => '%贷%款',
            '逾期' => '%逾%期',
            '借款' => '%借%款',
            '信用' => '%信%用',
            '征信' => '%征%信',
        );
        foreach($k_arr as $k => $v){
            $content = str_replace($k, $v, $content);
        }
        $result = $this->_client->sendSMS($mobile,$content.'【易商金融】');
        return $result;
    }

    public function curl($url, $httpMethod = "GET", $postFields = null, $headers = null) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if ($this->readTimeout) {
            curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout);
        }
        if ($this->connectTimeout) {
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
        }
        //https request
        if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        if (is_array($headers) && 0 < count($headers)) {
            //curl_setopt($ch, CURLOPT_HEADER, true);
            $httpHeaders = self::getHttpHearders($headers);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
        }
        if ($httpMethod == "POST") {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
        }
        $response = curl_exec($ch);
        if (curl_errno($ch)) {
            //throw new Exception(curl_error($ch), 0);
            return false;
        } else {
            $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            if (200 !== $httpStatusCode) {
                //throw new Exception($response, $httpStatusCode);
                return false;
            }
        }
        curl_close($ch);
        return $response;
    }

    static function getHttpHearders($headers) {
        $httpHeader = array();
        foreach ($headers as $key => $value) {
            array_push($httpHeader, $key . ":" . $value);
        }
        return $httpHeader;
    }

}
